package dictcn.com.activity;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import dictcn.com.R;
import dictcn.com.R.id;
import dictcn.com.R.layout;
import dictcn.com.control.MyDatabaseHelper;
import dictcn.com.control.MytabOperate;
import dictcn.com.model.BookName;
import dictcn.com.model.LocalLibrary;


public class ShowWordContent extends ActivityGroup {//显示生词本中单词的基本信息activity

	private TextView txt_content=null;
	private TextView txt_name=null;
	private ImageButton btn_getIn=null;
	private String bookNames[]=null;
	private List<BookName> list=null;
	private SQLiteOpenHelper helper=null;
	private MytabOperate mytab=null;
	private LocalLibrary local=null;
	
	//生词本的在集合中的索引
	private int index=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.showcontent);
		
		initView();
	}
	
	public void initView(){
		
		txt_content=(TextView)findViewById(R.id.txt_contentShow);
		txt_name=(TextView)findViewById(R.id.txt_nameShow);
		btn_getIn=(ImageButton)findViewById(R.id.btn_getIn);
		
		local=(LocalLibrary)getIntent().getBundleExtra("bundle").getSerializable("word");
		txt_name.setText(local.getLetter_name());
		String str="美式音标："+local.getFanyi_am()+"\n"+"英式发音："+local.getFanyi_en()+"\n\n"+
				"释义："+local.getInterpret()+"\n\n"+"拼音："+local.getPingyin();
		txt_content.setText(str);
		helper=new MyDatabaseHelper(ShowWordContent.this);
		 helper.getWritableDatabase();
			mytab=new MytabOperate(
			    	helper.getWritableDatabase());
		list=new ArrayList<BookName>();
		list=mytab.SelctAllBookName();
		int size=list.size();
		bookNames=new String[size];
		for(int i=0;i<size;i++){
			bookNames[i]=list.get(i).getB_name();
		}
		
		//添加到生词本按钮监听
		btn_getIn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				index=0;
				//自定义对话框的显示
				Dialog dlg=new AlertDialog.Builder(ShowWordContent.this)
						.setTitle("添加到生词本")
						.setSingleChoiceItems(bookNames, 0, new DialogInterface.OnClickListener() {
							
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								index=which;
							}
						})
						.setPositiveButton("确定", new DialogInterface.OnClickListener() {
							
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								
								//添加单词到生词本的数据操作
								
								helper=new MyDatabaseHelper(ShowWordContent.this);
								 helper.getWritableDatabase();
									mytab=new MytabOperate(
									    	helper.getWritableDatabase());
								mytab.createBook(list.get(index).getB_id(), local.getL_id(), 
											new SimpleDateFormat("yyyy-MM-dd").format(new Date()), 5);						
								Toast.makeText(ShowWordContent.this, "生词添加成功", Toast.LENGTH_SHORT).show();
							}
						})
						.setNegativeButton("取消", null)
						.create();
				dlg.show();
			}
		});
	}
	
	//返回键的监听
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK)
		{
			ShowWordContent.this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
